xpk.Growl = function(options){
	var _ready = 0;
	
	var _position = function(){
		if (!window.ie6) return;
        var pos = _this.options.position;
		var _window = $(window);
        var top = pos.vertical == 'bottom' ? _window.scrollTop() + _window.height() - _this.wrapper.height() - _this.options.offset : _window.scrollTop() + _this.options.offset;
        _this.wrapper.css('top', top < 0 ? 0 : top);
		_this.wrapper.css('bottom', '');
	};
	
	
	var _this = {
		active: 0,
		stack: [],
		messageLog: [],
		messagesCounter: 0,
		options:{
			speed: 500,
			hideDelay: 50000,
			offset: 10,
			status: 'working',
			fixed: false,
			single: false,
			maxMessages: 8,
			className: 'growl-message-container',
			position: {
				vertical: 'bottom',
				horisontal: 'right'
			}
		},
		init: function(options){
			this.setOptions(options || {});
			if(window.ie6){
				this.options.speed = 0;
			};
			if (this.options.initialize) this.options.initialize.call(this);
			return this;
		},
		createMarkup: function(){
			var styles = {
				'position': (window.ie6 ? 'absolute' : 'fixed'),
				'z-index': '2000',
				'display': 'block',
				'overflow': 'hidden'
			};
			
			var position = _this.options.position;
	
			styles[position.vertical] = _this.options.offset;
			styles[position.horisontal] = _this.options.offset;
	
			var class_name = _this.options.className + ' ' + position.vertical + '-' + position.horisontal;
			
			this.wrapper = $('<div/>').appendTo("body").css(styles).addClass(class_name);

			this.wrapper.delegate("a.close-message", "click", function(){
				var $messageBlock = $(this).closest('div.growl-message-wrapper', _this.wrapper);
				if($messageBlock.length) {
					_this.hide($messageBlock);
				};
				return false;
			});

			this.wrapper.delegate("div.growl-message-wrapper", "contextmenu", function(){
				var $messageBlock = $(this);
				if($messageBlock.length) {
					_this.hide($messageBlock);
				};
				return false;
			});


			this.msgTemplate = $('<div class="growl-message-wrapper">\
				<div class="growl-message">\
					<a href="#" class="close-message"><!-- --></a>\
					<div class="message"></div>\
				</div>\
			</div>').css('display','none');
			
			_ready = 1;
		},
		alert: function(options) {
/*
	options = {
		title:
		message:
		status
	}
*/
			if (!_ready){
				this.createMarkup();
			}
			
			var position = this.options.position;
	
			var injectFoo = position.vertical == 'bottom' ? 'prependTo' : 'appendTo';
			
//			this.wrapper.removeClass(this.options.className).addClass(this.options.className);

			if (this.options.single) {
				this.wrapper.children().remove();
			};
			
			var $messageBlock = this.msgTemplate.clone()[injectFoo](this.wrapper);
			$messageBlock.find('div.message').html((options.title ? '<h4>' + options.title + '</h4>' : '') + options.message)
			
			if(options.status){
				$messageBlock.addClass(options.status);
			};
			
			$messageBlock.timer = window.setTimeout(function(){
				_this.hide($messageBlock);
				delete $messageBlock.timer;
			}, options.delay || this.options.hideDelay);

			this.$messages = this.wrapper.children();
			
			if(this.$messages.length > this.options.maxMessages){
				this.hide(this.$messages.eq(0));
			};
	
			if (window.ie6) {
				_position();
				if (this.$messages.length == 1) {
					$(window).bind("scroll resize", _position);
				};
			};
	
			$messageBlock.fadeTo(this.options.speed, 1, _position);
			
			if(this.options.onshow){
				this.options.onshow.apply(this, [$messageBlock]);
			};
			
			return this;
		},
		
		cancel: function(delay){
			window.setTimeout(function(){
				_this.hide()
			}, delay || 5);
		},
		
		hide: function($msg){
			var $messages = this.wrapper.children();
			if($msg) {					
				$msg.hide(this.options.speed/2, function() {
					$(this).remove();
					var messagesCount = $messages.length;
					if (messagesCount == 0 && window.ie6){
						$(window).unbind('scroll resize', _position);
					};
					_position();
				});
			} else {
				$messages.each(function(i){
					var el = $(this);
					_this.hide(el)
				});
			};
			if(this.options.onhide){
				this.options.onhide.apply(this);
			};
			
			return this;
		},
		setOptions: function(options){
			this.options = $.extend(this.options, options || {});
			return this;
		}
		
	};

	return _this.init(options);

};